package com.applet.applet_shangcheng.controller;

import com.applet.applet_shangcheng.entity.IntegralOrder;
import com.applet.applet_shangcheng.service.IntegralOrderService;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.applet.applet_shangcheng.utlis.QueryPageBean;
import com.applet.applet_shangcheng.utlis.Result;
import com.applet.applet_shangcheng.utlis.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/integralOrder")
@SuppressWarnings("unchecked")
@CrossOrigin
@Api(value="积分商城订单管理",tags={"积分商城订单管理"})
public class IntegralOrderController {

    @Autowired
    private IntegralOrderService integralOrderService;
    @ApiOperation(value="_修改订单成功")
    @PostMapping("/updateOrder")
    public Result updateOrder(@RequestBody IntegralOrder integralOrder) {
        int result = integralOrderService.updateOrder(integralOrder);
        if (result>0){
            return new Result(true, StatusCode.OK,"修改订单成功");
        }
        return new Result(false, StatusCode.ERROR,"修改订单失败");
    }
    @ApiOperation(value="_修改订单成功")
    @PostMapping("/updateOrders")
    public Result updateOrders(@RequestBody IntegralOrder integralOrder) {
        int result = integralOrderService.updateOrders(integralOrder);
        if (result>0){
            return new Result(true, StatusCode.OK,"修改订单成功");
        }
        return new Result(false, StatusCode.ERROR,"修改订单失败");
    }
    @ApiOperation(value="_分页查询所有订单")
    @PostMapping("/findAlls")
    public PageResult findAlls(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = integralOrderService.findAlls(queryPageBean.getQueryString(),queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        return pageResult;
    }
    ;
    @ApiOperation(value="WX_根据username查询订单")
    @GetMapping("/findAll/{username}")
    public Result findAll(@PathVariable("username") String username) {
        List<IntegralOrder> orderList = integralOrderService.findAll(username);
        return new Result(true, StatusCode.OK,"查询所有订单成功",orderList);
    }
    ;
    @ApiOperation(value="_根据orderId查询订单")
    @GetMapping("/findById/{orderId}")
    public Result findById(@PathVariable() String orderId) {
        Map<String,Object> map = integralOrderService.findById(orderId);
        return new Result(true, StatusCode.OK,"查询订单成功",map);
    }

    @ApiOperation(value="_根据id查询订单")
    @GetMapping("/findByIdOrder/{id}")
    public Result findByIdOrder(@PathVariable() String id) {
        IntegralOrder integralOrder  = integralOrderService.findByIdOrder(id);
        return new Result(true, StatusCode.OK,"查询订单成功",integralOrder);
    }
    ;
    @ApiOperation(value="_根据id删除订单")
    @GetMapping("/deleteOrder/{id}")
    public Result deleteOrder(@PathVariable("id") int id) {
        int result = integralOrderService.deleteOrder(id);
        if (result>0){
            return new Result(true, StatusCode.OK,"删除订单失败");
        }
        return new Result(false, StatusCode.ERROR,"删除订单失败");
    }
    /*
    goodsId:商品id
    sum_number:商品总个数
`   username'用户名称',
  ` consignee` '收货人',
  ` phone'收货人手机号',
    `consignee_address收货地址',
     */
   /* @PostMapping("/exchange")
    public Result exchangeOrder(@RequestParam(value = "goodsId")String goodsId,
                                @RequestParam(value = "sumNumber")int sumNumber,
                                @RequestParam(value = "username")String username,
                                @RequestParam(value = "consignee")String consignee,
                                @RequestParam(value = "phone")int phone,
                                @RequestParam(value = "consigneeAddress")String consigneeAddress
                                ){
        IntegralOrder integralOrder1 = integralOrderService.exchangeOrder(goodsId,sumNumber,username,consignee,phone,consigneeAddress);
        if (integralOrder1!=null){
            return new Result(true,StatusCode.OK,"添加购物车成功",integralOrder1);
        }
        return new Result(false,StatusCode.ERROR,"添加购物车失败");
    }*/

    @ApiOperation(value="WX_添加积分商品订单")
    @PostMapping("/exchange")
    public Result exchangeOrder(@RequestBody  IntegralOrder integralOrder) {
        Map<String,Object> map = integralOrderService.exchangeOrder(integralOrder);
        if (map!=null){
            return new Result(true,StatusCode.OK,"添加订单成功",map);
        }
        return new Result(false,StatusCode.ERROR,"添加订单失败");
    }
}
